﻿using System;
using AMM.Business;
using AMM.Model;
using AMM.Model.Project;
using System.Collections.Generic;

namespace AMM.UserControls {
    public partial class ProductSearchControl : BaseSearchControl {

        public override BaseSearchModel GetSearchModel() {
            int currencyValue = Convert.ToInt32(this.cmbProductType.SelectedValue);
            int enabledValue = Convert.ToInt32(this.cmbIsEnabled.SelectedValue);
            int selfFinishedValue = Convert.ToInt32(this.cmbIsSelfFinished.SelectedValue);

            ProductSearchModel searchResult = new ProductSearchModel();
            searchResult.Code = this.tbCode.Text;
            searchResult.Name = this.tbName.Text;
            searchResult.ProductTypeId = currencyValue;
            searchResult.IsEnabled = Convert.ToBoolean(enabledValue);
            searchResult.IsSelfFinished = Convert.ToBoolean(selfFinishedValue);

            if (currencyValue == 0) { searchResult.ProductTypeId = null; }
            if (enabledValue == -1) { searchResult.IsEnabled = null; }
            if (selfFinishedValue == -1) { searchResult.IsSelfFinished = null; }

            return searchResult;
        }

        public override void ResetSearchModel() {
            this.tbCode.Clear();
            this.tbName.Clear();
            this.cmbProductType.SelectedIndex = 0;
            this.cmbIsEnabled.SelectedValue = "-1";
            this.cmbIsSelfFinished.SelectedValue = "-1";
        }

        public ProductSearchControl() {
            this.InitializeComponent();           
        }

        private void ProductSearchControl_Load(object sender, EventArgs e) {
            this.BindBasics(this.cmbProductType, BasicsType.ProductType);
            this.BindIsEnabled(this.cmbIsEnabled);
            this.BindIsSelfFinished();
        }

        protected void BindIsSelfFinished() {
            List<object> resultList = new List<object>();
            resultList.Add(new { Text = "", Value = "-1" });
            resultList.Add(new { Text = "是", Value = "1" });
            resultList.Add(new { Text = "否", Value = "0" });

            this.cmbIsSelfFinished.DataSource = resultList;
            this.cmbIsSelfFinished.DisplayMember = "Text";
            this.cmbIsSelfFinished.ValueMember = "Value";
        }
    }
}
